package com.charger.controller;

import java.text.ParseException;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.charger.common.utils.DateUtils;
import com.charger.mapper.ChargerOperationMiddleMapper;
import com.charger.utils.QDateUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.domain.ChargerOperation;
import com.charger.service.IChargerOperationService;
import com.charger.common.utils.poi.ExcelUtil;

/**
 * 统计每日充电信息Controller
 * 
 * @author Qinkunpeng
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/operation/operation")
public class ChargerOperationController extends BaseController
{
    @Autowired
    private IChargerOperationService chargerOperationService;

    @Autowired
    private ChargerOperationMiddleMapper chargerOperationMiddleMapper;

    /**
     * 查询统计每日充电信息列表
     */
    @PreAuthorize("@ss.hasAnyPermi('operation:operation:list,statistic:operation:list')")
    @GetMapping("/list")
    public Object list(ChargerOperation chargerOperation) throws ParseException {
        startPage();
        List<ChargerOperation> list = chargerOperationService.selectChargerOperationList(chargerOperation);
        Map<String, ChargerOperation> map = new HashMap<>();
        list.forEach(chargerOperation1 -> {
            map.put(DateUtils.parseDateToStr("yyyy-MM-dd",
                    chargerOperation1.getCreateTime()), chargerOperation1);
        });
        Object o = null;
        if (chargerOperation.getParams() != null) o = chargerOperation.getParams().get("endCreateTime");
        Date endTime = null;
        Date startTime = null;
        if (o != null) {
            endTime = DateUtils.parseDate((String) chargerOperation.getParams().get("endCreateTime"), "yyyy-MM-dd");
            startTime = DateUtils.parseDate((String) chargerOperation.getParams().get("beginCreateTime"), "yyyy-MM-dd");
        }else{
            endTime = list.get(0).getCreateTime();
            startTime = list.get(list.size() - 1).getCreateTime();
        }
        long start = startTime.getTime();
        long end = endTime.getTime();
        long diff = (end - start) / (1000 * 3600 * 24);
        List<String> before = new ArrayList<>();
        List<Double> moneyData = new ArrayList<>();
        List<Double> chargeData = new ArrayList<>();
        if (diff <= 10){
            before = QDateUtils.getBefore((int) (diff + 1), endTime);
        }else{
            before = QDateUtils.getBefore(10, endTime);
        }
        before.forEach(b -> {
//            System.out.println(b);
            ChargerOperation chargerOperation1 = map.get(b);
            if (chargerOperation1 != null) {
                moneyData.add(chargerOperation1.getChargeMoney());
                chargeData.add(chargerOperation1.getCharges());
            }else{
                moneyData.add(0d);
                chargeData.add(0d);
            }
        });
        Map<String, Object> result = new HashMap<>();
        result.put("cdate", before);
        result.put("moneyData", moneyData);
        result.put("chargeData", chargeData);
        result.put("page", new PageInfo<ChargerOperation>(list));
        return AjaxResult.success(result);
    }

    /**
     * 导出统计每日充电信息列表
     */
    @PreAuthorize("@ss.hasPermi('operation:operation:export')")
    @Log(title = "统计每日充电信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChargerOperation chargerOperation)
    {
        List<ChargerOperation> list = chargerOperationService.selectChargerOperationList(chargerOperation);
        ExcelUtil<ChargerOperation> util = new ExcelUtil<ChargerOperation>(ChargerOperation.class);
        util.exportExcel(response, list, "统计每日充电信息数据");
    }

    /**
     * 获取统计每日充电信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('operation:operation:query')")
    @GetMapping(value = "/{opId}")
    public AjaxResult getInfo(@PathVariable("opId") Long opId)
    {
        return success(chargerOperationService.selectChargerOperationByOpId(opId));
    }

    /**
     * 新增统计每日充电信息
     */
    @PreAuthorize("@ss.hasPermi('operation:operation:add')")
    @Log(title = "统计每日充电信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChargerOperation chargerOperation)
    {
        return toAjax(chargerOperationService.insertChargerOperation(chargerOperation));
    }

    /**
     * 修改统计每日充电信息
     */
    @PreAuthorize("@ss.hasPermi('operation:operation:edit')")
    @Log(title = "统计每日充电信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChargerOperation chargerOperation)
    {
        return toAjax(chargerOperationService.updateChargerOperation(chargerOperation));
    }

    /**
     * 删除统计每日充电信息
     */
    @PreAuthorize("@ss.hasPermi('operation:operation:remove')")
    @Log(title = "统计每日充电信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{opIds}")
    public AjaxResult remove(@PathVariable Long[] opIds)
    {
        return toAjax(chargerOperationService.deleteChargerOperationByOpIds(opIds));
    }
}
